﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using FancyBattles.GameAudio;
using Microsoft.Xna.Framework.Media;
using FancyBattles.GameEntities;

namespace FancyBattles.GameScreen
{
    class ScreenManager : DrawableGameComponent
    {
        #region Atributes

        GameScreen activeScreen;
        List<GameScreen> screens = new List<GameScreen>();
        MainMenuScreen mainMenuScreen = new MainMenuScreen();
        GameplayScreen gamePlayScreen = new GameplayScreen();
        TutorialScreen tutorialScreen = new TutorialScreen();
        CreditsScreen creditsScreen = new CreditsScreen();
        PauseScreen pauseScreen = new PauseScreen();
        GameOverScreen gameOverScreen = new GameOverScreen();
        SpriteBatch spriteBatch;
        SpriteFont font;
        GameScreen lastScreen;
        AudioManager audioManager;

        #endregion

        #region Properties

        public SpriteBatch SpriteBatch
        {
            get { return spriteBatch; }
        }

        public SpriteFont Font
        {
            get { return font; }
        }

        #endregion

        #region Creators

        public ScreenManager(Game game) : base(game)
        {
            this.activeScreen = mainMenuScreen;

            this.mainMenuScreen.ScreenManager = this;
            this.gamePlayScreen.ScreenManager = this;
            this.tutorialScreen.ScreenManager = this;
            this.creditsScreen.ScreenManager = this;
            this.pauseScreen.ScreenManager = this;
            this.gameOverScreen.ScreenManager = this;
            this.mainMenuScreen.AudioManager = audioManager;
            this.gamePlayScreen.AudioManager = audioManager;
            this.tutorialScreen.AudioManager = audioManager;
            this.creditsScreen.AudioManager = audioManager;
            this.pauseScreen.AudioManager = audioManager;
            this.gameOverScreen.AudioManager = audioManager;
            this.screens.Add(mainMenuScreen);
            this.screens.Add(gamePlayScreen);
            this.screens.Add(tutorialScreen);
            this.screens.Add(creditsScreen);
            this.screens.Add(pauseScreen);
            this.screens.Add(gameOverScreen);
        }

        #endregion

        #region Methods

        public override void Initialize()
        {
            base.Initialize();
        }

        protected override void LoadContent()
        {
            ContentManager content = Game.Content;

            spriteBatch = new SpriteBatch(GraphicsDevice);
            font = content.Load<SpriteFont>("MenuFont");

            audioManager = new AudioManager(Game);
            audioManager.PlayMusicLoop("Music/Nariga_01");

            foreach (GameScreen screen in screens)
            {
                screen.LoadContent();
            }
        }

        protected override void UnloadContent()
        {
            foreach (GameScreen screen in screens)
            {
                screen.UnloadContent();
            }
        }

        public override void Draw(GameTime gameTime)
        {
            activeScreen.Draw(gameTime);
        }


        public override void Update(GameTime gameTime)
        {
            switch (activeScreen.Update(gameTime))
            {
                case UpdateResponse.CLOSE_SCREEN:
                    SwitchScreen();
                    break;
                case UpdateResponse.KEEP_STATE:
                    //keep rolling
                    if (activeScreen.GetType() == typeof(PauseScreen))
                    {
                        activeScreen = lastScreen;
                    }
                    lastScreen = activeScreen;
                    break;
                case UpdateResponse.PAUSE_GAME:
                    //set pause screen as active
                    //save old screen
                    activeScreen = pauseScreen;
                    break;
                case UpdateResponse.EXIT_GAME:
                    Game.Exit();
                    break;
                default:
                    break;
            }

            InputHandler.Update();
        }

        private void SwitchScreen()
        {
            //stupid code, refactor using somethig clever
            if (activeScreen is MainMenuScreen)
            {
                if (MainMenuScreen.mainMenuEntries.Find(MainMenuScreen.singlePlayerEntry).Value.IsSelected)
                {
                    GameplayScreen.isSinglePlayer = true;
                    gamePlayScreen.StartGame();
                    activeScreen = gamePlayScreen;
                    audioManager.PlayMusicLoop("Music/Nariga_03");
                }
                else if (MainMenuScreen.mainMenuEntries.Find(MainMenuScreen.multiPlayerEntry).Value.IsSelected)
                {
                    GameplayScreen.isSinglePlayer = false;
                    gamePlayScreen.StartGame();
                    activeScreen = gamePlayScreen;
                    audioManager.PlayMusicLoop("Music/Nariga_03");
                }
                else if (MainMenuScreen.mainMenuEntries.Find(MainMenuScreen.tutorialEntry).Value.IsSelected)
                {
                    TutorialScreen.currentScreen = TutorialScreen.tutorialScreens.First.Value;
                    activeScreen = tutorialScreen;
                }
                else
                {
                    activeScreen = creditsScreen;
                }
            }
            else
            {
                if (activeScreen is GameplayScreen)
                {
                    BouncingBitch.DeleteBitches();
                    activeScreen = gameOverScreen;
                }
                else if (activeScreen is PauseScreen)
                {
                    BouncingBitch.DeleteBitches();
                    activeScreen = mainMenuScreen;
                    audioManager.PlayMusicLoop("Music/Nariga_01");
                }
                else if (activeScreen is TutorialScreen || activeScreen is CreditsScreen)
                {
                    activeScreen = mainMenuScreen;
                }
                else if (activeScreen is GameOverScreen)
                {
                    activeScreen = mainMenuScreen;
                    audioManager.PlayMusicLoop("Music/Nariga_01");
                }
            }
        }

        #endregion
    }
}
